package com.zzx.boot.servlet.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.boot.constant.GlobalEnum;
import com.zzx.boot.entity.dto.SupportDto;
import com.zzx.boot.entity.table.LoginUser;
import com.zzx.boot.entity.table.Post;
import com.zzx.boot.entity.table.Support;
import com.zzx.boot.entity.vo.SupportVo;
import com.zzx.boot.exception.SystemctlException;
import com.zzx.boot.mapper.SupportMapper;
import com.zzx.boot.servlet.PostService;
import com.zzx.boot.servlet.SupportService;
import com.zzx.boot.utils.DateTImeUtils;
import com.zzx.boot.utils.SecurityUtils;
import org.hibernate.validator.internal.util.stereotypes.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author tan
 * @version 1.0
 * @date 2023/7/10 12:13
 */
@Service
public class SupportServiceImpl extends ServiceImpl<SupportMapper, Support> implements SupportService {

    @Lazy
    @Resource
    private PostService postService;

    @Override
    public String createSupport(SupportDto supportDto) {
        // 判断该文章是否存在
        Post post = postService.getById(supportDto.getPostId());
        if(Objects.isNull(post)){
            throw new SystemctlException(GlobalEnum.PARAMS_INVALID);
        }
        Support support = new Support(null, SecurityUtils.getUserId(),
                supportDto.getPostId(),supportDto.getType(), DateTImeUtils.getDate());
        int insert = baseMapper.insert(support);
        if(insert==1&&supportDto.getType()==1){
            return "点赞成功";
        }
        return "您踩了它";
    }

    @Override
    public Map<Long, SupportVo> getBetchByUserId(LoginUser loginUser) {
        if(Objects.isNull(loginUser)) {// 未登录
            return new HashMap<>();
        }
        LambdaQueryWrapper<Support> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Support::getUserId,loginUser.getUser().getId());
        List<Support> list = baseMapper.selectList(queryWrapper);
        Map<Long, SupportVo> map = list.stream().collect(Collectors.toMap(item -> item.getPostId(), item -> new SupportVo(item.getId().toString(),
                item.getUserId().toString(),item.getPostId().toString(),item.getType())));
        return map;
    }

    @Override
    public SupportVo getUserId(LoginUser loginUser,Long postId) {
        if(Objects.isNull(loginUser)) {// 未登录
            return null;
        }
        LambdaQueryWrapper<Support> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Support::getUserId,loginUser.getUser().getId());
        queryWrapper.eq(Support::getPostId,postId);
        Support support = baseMapper.selectOne(queryWrapper);
        if(Objects.isNull(support))return  null;
        return new SupportVo(support.getId().toString(),
                support.getUserId().toString(),
                support.getPostId().toString(),
                support.getType());
    }
}
